package net.lab1024.sa.admin.module.business.sp.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.shop.dao.ShopDao;
import net.lab1024.sa.admin.module.business.shop.domain.Enity.ShopEntity;
import net.lab1024.sa.admin.module.business.sp.dao.SpDao;
import net.lab1024.sa.admin.module.business.sp.domain.SpVO;
import net.lab1024.sa.admin.module.business.sp.domain.SpEntity;
import net.lab1024.sa.admin.module.business.sp.domain.form.SpAddForm;
import net.lab1024.sa.admin.module.business.sp.domain.form.SpQueryForm;
import net.lab1024.sa.admin.module.business.sp.domain.form.SpUpdateForm;
import net.lab1024.sa.admin.module.system.type.Dao.TypeDao;
import net.lab1024.sa.admin.module.system.type.domain.TypeEntity;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SpService {
    @Resource
    private SpDao spDao;
    @Resource
    private TypeDao typeDao;
    @Resource
    private ShopDao shopDao;
    public ResponseDTO<PageResult<SpVO>> querySpPage(SpQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<SpEntity> entityList = spDao.queryByPage(page, queryForm);
        List<ShopEntity> shopList = shopDao.selectList(new QueryWrapper<ShopEntity>().eq("is_del", 0));
        List<TypeEntity> typeList = typeDao.selectByKey("shop_sign");
        PageResult<SpVO> pageResult = SmartPageUtil.convert2PageResult(page, entityList, SpVO.class);
        for (SpVO spVO : pageResult.getList()) {
            for (ShopEntity shopEntity : shopList) {
                if (spVO.getShopId().equals(shopEntity.getShopId())) {
                    spVO.setShopName(shopEntity.getShopName());
                }
            }
            for (TypeEntity typeEntity : typeList) {
                if (spVO.getSpType().equals(typeEntity.getTypeId())) {
                    spVO.setSpTypeName(typeEntity.getTypeName());
                }
            }
        }
        return ResponseDTO.ok(pageResult);
    }
    public ResponseDTO<String> add(SpAddForm spAddDTO) {
        List<SpEntity> alreadyEntity = spDao.selectByName(String.valueOf(spAddDTO.getSpName()));
        SpEntity entity = SmartBeanUtil.copy(spAddDTO, SpEntity.class);
        if (alreadyEntity.contains(entity)) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST);
        }
        entity.setCreateTime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        entity.setCreateBy("syf");
        entity.setUpdateBy("syf");
        spDao.insert(entity);
        return ResponseDTO.ok();
    }
    public ResponseDTO<String> updateSp(SpUpdateForm updateDTO) {
        Long configId = updateDTO.getSpId();
        SpEntity entity = spDao.selectById(configId);
        if (null == entity) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        entity = SmartBeanUtil.copy(updateDTO, SpEntity.class);
        List<SpEntity> alreadyEntity = spDao.selectByName(updateDTO.getSpName());
        if (null != alreadyEntity && alreadyEntity.contains(entity)) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, "商品 已存在");
        }

        // 更新数据
//        entity = SmartBeanUtil.copy(updateDTO, SpEntity.class);
        spDao.updateById(entity);
        return ResponseDTO.ok();
    }
    public ResponseDTO<String> deleteSp(SpUpdateForm updateDTO) {
        Long configId = updateDTO.getSpId();
        SpEntity entity = spDao.selectById(configId);
        if (null == entity) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        // 更新数据
        entity = SmartBeanUtil.copy(updateDTO, SpEntity.class);
        entity.setDel(true);
        spDao.updateById(entity);
        return ResponseDTO.ok();
    }


}
